package com.dreamana.components
{
	import com.dreamana.ui.BaseComponent;
	import com.dreamana.ui.ISkinnable;
	import com.dreamana.ui.SkinGraphics;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;
	
	public class ToggleButton extends BaseComponent implements ISkinnable
	{		
		protected var _upState:Shape;
		protected var _upStateTexture:BitmapData;
		protected var _upState9Grid:Rectangle;
		
		protected var _downState:Shape;
		protected var _downStateTexture:BitmapData;
		protected var _downState9Grid:Rectangle;
		
		protected var _label:DisplayObject;
		protected var _selected:Boolean = false;
		
		public function ToggleButton(w:int, h:int, label:DisplayObject=null)
		{
			_width = w;
			_height = h;
			_label = label;
			
			//default setting
			this.buttonMode = true;
			
			this.init();
		}
		
		override protected function addChildren():void
		{
			_upState = new Shape();
			this.addChild(_upState);
			
			_downState = new Shape();
			this.addChild(_downState);
			_downState.visible = false;
						
			if(_label) this.addChild(_label);
			
			this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		//--- PUBLIC METHODS ---
		
		override public function draw():void
		{
			var g:Graphics;
						
			g =_upState.graphics;
			g.clear();
			if(_upStateTexture) {
				if(_upState9Grid) SkinGraphics.fill9Grid(g, _upStateTexture, _width, _height, _upState9Grid, false);
				else SkinGraphics.fillRect(g, _upStateTexture, _width, _height);
			}
			else {
				SkinGraphics.drawWireFrame(g, _width, _height);
			}
			
			g = _downState.graphics;
			g.clear();
			if(_downStateTexture) {
				if(_downState9Grid) SkinGraphics.fill9Grid(g, _downStateTexture, _width, _height, _downState9Grid, false);
				else SkinGraphics.fillRect(g, _downStateTexture, _width, _height);
			}
			else {
				SkinGraphics.drawWireFrame(g, _width, _height);
			}
		}
		
		override public function setStyle(style:String, value:Object):void
		{
			super.setStyle(style, value);
			
			switch(style) {
				case "upstate-image":
					_upStateTexture = value as BitmapData;
					invalidate();
					break;
				case "upstate-9grid":
					_upState9Grid = value as Rectangle;
					invalidate();
					break;
				case "downstate-image":
					_downStateTexture = value as BitmapData;
					invalidate();
					break;
				case "downstate-9grid":
					_downState9Grid = value as Rectangle;
					invalidate();
					break;
			}
		}
		
		//--- EVENT HANDLERS ---
				
		protected function onClick(e:MouseEvent):void
		{
			this.selected = !this.selected;
		}
		
		//--- GETTER/SETTERS ---
		
		public function get label():DisplayObject { return _label; }
		public function set label(value:DisplayObject):void
		{
			if(_label && this.contains(_label)) this.removeChild(_label);
			
			_label = value;
			this.addChild(_label);
		}
		
		protected var f0:Array = [];
		protected var f1:Array = [new ColorMatrixFilter([0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0,0,0,1,0])];
		
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			
			this.buttonMode = value;
			this.filters = value? f0 : f1;
		}
		
		public function get selected():Boolean { return _selected; }
		public function set selected(value:Boolean):void {
			_selected = value;
			if(_selected) {
				_downState.visible = true;
				_upState.visible = false;
			}
			else {
				_downState.visible = false;
				_upState.visible = true;
			}
		}
	}
}